package net.tp.struct.adt;

import java.util.*;

public class ListPrinter {

    public static <E> String toString(SinglyNode<E> list) {
        StringBuilder result = new StringBuilder();
        Map<SinglyNode<E>, Boolean> visited = new IdentityHashMap<>();
        for (SinglyNode<E> i = list; i != null; i = i.next) {
            if (!visited.containsKey(i)) {
                visited.put(i, true);
                if (result.length() > 0) result.append(" → ");
                result.append(i.element);
            }
            else {
                result.append(" ↻ ") .append(i.element);
                break;
            }
        }

        return result.toString();
    }
}
